define([
	'../libs/zepto',
	'../common/base',
	'../common/ajax',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/storage',
	'../common/agentInfo',
	'../common/api'
], function() {
	this.init = function() {
		select_tizhuan(); //选取支付方式
		_getVideoMsg(); //获得科目章节
		_getVideoList(); //获取打包购买
		_getSelectType(); //得到所选方式
		var dangeP = '';
		var dabaoP = ''; //打包价格

	}

	//选取支付方式
	function select_tizhuan() {
		//卡片类型
		var iS_check = false;
		$(document).on('click', '.select_way p', function() {
			if(iS_check == false) {
				$('.duigou').css('display', 'none');
				$('.noduigou').css('display', 'inline-block');
				$('.mprice h2').text('￥' + dabaoP);
				iS_check = true;
			} else {
				$('.duigou').css('display', 'inline-block');
				$('.noduigou').css('display', 'none');
				$('.mprice h2').text('￥' + dangeP);
				iS_check = false;
			}
		})

		//支付方式
		$(document).on('click', '.select_way .wayPay', function() {
			$(this).addClass('checked').siblings().removeClass('checked');

		});
		//确认购买，选择跳转路径
		$(document).on('click', '.payBtn', function() {
			_getOrderID();
		})
	};
	//获得价格
	function _getVideoMsg() {
		//获取英文名
		var bookID = G_Prg.getQueryString('BookID');
		var getAppEName = G_Cookie.getAppEName();
		var onlyCode = G_Prg.getQueryString('OnlyCode');
		var chapterID = G_Prg.getQueryString('ChapterID');
		var url = '/api/videoTiny/getVideoMsg?OnlyCode=' + onlyCode + '&bookID=' + bookID + '&chapterID=' + chapterID;
		var success = function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				//输出科目名称
				var appName = G_Prg.getQueryString('appName');
				if(!appName || appName.toString().trim() === '') {
					appName = G_Cookie.getAppName();
				}
				var chapterName = G_Prg.getQueryString('chapterName');
				var chapterName = chapterName ? '—' + unescape(chapterName) : '';
				dangeP = jsonData.data;
				//获取当前所选科目
				var curLesson = '<div class="curBox"><p>当前科目</p><span class="curBook">' + appName + chapterName + '</span></div>'
				var html = '<li class="mprice" style="margin-left:3px;"><h2>￥' + dangeP + '</h2><h3>有效期至考试结束</h3></li>';
				var html0 = '<p>选择充值</p><ul class="payList"></ul>'
				var html1 = '<p><img src="/images/check1.png" class="duigou" style="display:inline-block"><img src="/images/checked1.png" class="noduigou" style="display:none"><span class="dabao">打包购买</span></p><p style="margin-top:6%">支付方式</p><ul class="wayList"><li class = "wayPay checked" value="1" style="background-position: 10px 10px;"><div><img src="/images/alipay.png" alt=""><span class="ali" >支付宝支付</span></div></li>' +
					'<li class = "wayPay" value="2" style="background-position: 10px 10px;"><div><img src="/images/wechat.png" alt=""><span class="ali" >微信支付</span></div></li></ul>';
				var html2 = '<div class="payBtn">立即支付</div>';
				$('.a').html(curLesson);
				$(".select_pay").append(html0);
				$('.payList').append(html);
				$(".mprice").eq(0).addClass('checked');
				$('.select_way').append(html1);
				$('.b').append(html2);
				return;

				G_Prg.alert('价格获取失败');
			} else {
				G_Prg.alert('该书籍或章节未指定价格', function() {
					window.location.href = '/BasisChinese';
				});
				//alert('该书籍或章节未指定价格',function())
			}

		}
		G_AjaxApi_Record.get(url, true, success);

	}

	//获取打包购买的价钱
	function _getVideoList() {
		var bookID = G_Prg.getQueryString('BookID');
		if(!bookID) {
			return;
		}
		var success = function(result) {
			var jsonData = JSON.parse(result);
			dabaoP = jsonData.data;
			if(jsonData['status'] !== 200) {

				G_Prg.alert(jsonData['msg']);
				return;
			} else if(jsonData['status'] === 201) {
				G_Prg.throw('该书籍或章节未指定价格')
			}

		}
		G_AjaxApi_Record.get('/api/videoTiny/getVideoBook?userID=' + G_Cookie.getUserID() + '&bookID=' + bookID, true, success);
	}
	//选择的方式
	function _getSelectType() {
		var temp = document.getElementsByName("wayPay");
		for(var i = 0; i < temp.length; i++) {
			if(temp[i].checked) {
				var t = temp[i].value;
				return t;
			}
		}
		return null;
	}
	//支付跳转
	function _getOrderID() {
		var onlyCode = G_Prg.getQueryString('OnlyCode');
		var bookID = G_Prg.getQueryString('BookID');
		var chapterID = G_Prg.getQueryString('ChapterID');

		if($('h2').text() == '￥' + dangeP) {
			isPlural = false;
		} else {
			isPlural = true;
		}
		var type = '';
		if($('.wayList li.checked').attr('value')=='2'){
			type = 'android';
		}else{
			type = 'web_android';
		}
		var data = {
			userID: G_Cookie.getUserID(),
			//			onlyCode: onlyCode,
			type: type,
			bookID: bookID,
			//			guid: G_Cookie.getGuid(),
			chapterID: chapterID,
			isPlural: isPlural,
			backUrl: 'http://' + window.location.host + '/BasisChinese',
			returnUrl: 'http://yxgjp.ksbao.com/?'

		}
		var success = function(json) {
			var jsonData = JSON.parse(json);
			if($('.wayList li.checked').attr('value')=='2') {
//				var orderID = jsonData['data'].substring(16,33);
//				var price = jsonData['data'].substring(43,45);
//				console.log(orderID)
//				console.log(price)
				window.android.getParams(jsonData['data'])
			} else {
				window.location.href = jsonData['data'];
			}
		}
		G_AjaxApi_Record.post('/api/videoTiny/orderTiny', data, true, success);
	}
	//浏览器类型
	function _getClientType() {
		var browser = {
			versions: function() {
					var u = navigator.userAgent,
						app = navigator.appVersion;
					return {
						ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
						android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
						iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
						iPad: u.indexOf('iPad') > -1, //是否iPad
						webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
					};
				}
				()
		};
		if(browser.versions.iPhone || browser.versions.iPad) {
			return 'web_iPhone';
		}
		if(browser.versions.ios || browser.versions.android || browser.versions.webApp) {
			return 'web_android';
		}
		return 'web_pc';
	}

	function _getUserAgent() {
		var userAgent = navigator.userAgent.split('/');
		return userAgent;
	}
	init();
});